#!/bin/bash

DOCKER_NAME=houxin-nginx
DOCKER_FILE=Dockerfile

echo "DOCKER_NAME is ${DOCKER_NAME}"
DOCKER_IMAGE=`/usr/bin/docker images | grep ${DOCKER_NAME} | awk -F ' ' '{print $3}'`
if [ -n "${DOCKER_IMAGE}" ]; then
        # check docker container
        for dc in `/usr/bin/docker ps -a | grep ${DOCKER_NAME}  | awk -F " " '{print $1}'`
        do
                echo "Stop container: ${dc}"
                /usr/bin/docker stop ${dc}
                # delete while docker container was exists
                echo "##Delete exists Container_Id: "${dc}
                /usr/bin/docker rm ${dc}
        done

        # delete while docker image was exists
        echo "##Delete exists Image: "${DOCKER_IMAGE}
        /usr/bin/docker rmi ${DOCKER_IMAGE}
fi
echo "****delete finish*****"

echo "**Init dockerfile start***"

 /usr/bin/docker build  -f ${DOCKER_FILE}  -t ${DOCKER_NAME}  .

 /usr/bin/docker run -e TZ="Asia/Shanghai" --privileged  \
 -itd -h ${DOCKER_NAME}.com --name ${DOCKER_NAME} \
 -v  /opt/project/houxin/houxin-ui/${DOCKER_NAME}.conf:/etc/nginx/conf.d/default.conf \
-v  /opt/project/houxin/houxin-ui/dist:/usr/share/nginx/html \
-v   /opt/project/houxin/houxin-ui/logs:/var/log/nginx    \
-p 8045:80 --network=houxin-net  --ip 192.168.1.11  ${DOCKER_NAME}
